//
//  WithdrawalVC.m
//  BrushFace
//
//  Created by timer_q on 16/1/22.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "WithdrawalVC.h"
#import "MySceneModel.h"
#import "BorrowScenceModel.h"
#import "NavHelper.h"
#import "RegiestWXViewController.h"
#import "WeChat.h"
#import "AlertDialogHelper.h"
#import "TiXianSuccessVC.h"

@interface WithdrawalVC ()
{
    CGFloat yue;
    NSString *nickname;
}
@property (nonatomic,strong)MySceneModel *reqmodel;
@property (nonatomic,strong)BorrowScenceModel *scenceModel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *yuELabel;
@end

@implementation WithdrawalVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navBar setTitle:@"余额提现"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    self.tixianBtn.enabled = NO;
    _reqmodel = [MySceneModel SceneModel];
    _scenceModel = [BorrowScenceModel SceneModel];
        [_yuELabel addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.yuELabel.text = @"";
    self.titleLabel.text = @"微信零钱";
    
    [_reqmodel user_getAccountAndCB:^(id output) {
        self.yuELabel.placeholder = [NSString stringWithFormat:@"当前可提现余额 %@",[output objectForKey:@"amount"]];
        NSString *yueStr = [NSString stringWithFormat:@"%@",[output objectForKey:@"amount"]];
        yueStr = [yueStr stringByReplacingOccurrencesOfString:@"," withString:@""];
        yue = [yueStr floatValue];
        nickname = [output objectForKey:@"fullname"];
        if (!nickname.isNotEmpty) {
            nickname = @"未实名";
        }
        self.titleLabel.text = [NSString stringWithFormat:@"微信零钱 (%@)",nickname];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)tixianbtnClick:(id)sender {
    [self.view endEditing:YES];
    CGFloat tixianjine = [self.yuELabel.text floatValue];
    if (tixianjine<1) {
        [AlertDialogHelper showToast:@"最小提现金额为1"];
        return;
    }
    if (tixianjine>yue) {
        [AlertDialogHelper showToast:@"无法完成提现，余额不足"];
        return;
    }
    
    [_scenceModel common_fullnamed:^(BOOL isFull) {
        if (isFull == YES) {
            NSString *requsetstr = [NSString stringWithFormat:@"%@/user/isexistunionid.json",SERVICE_ADRESS];
            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            [manager GET:requsetstr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
                if ([[NSString stringWithFormat:@"%@",json[@"isExist"]] integerValue]==0) {
                    if (![WXApi isWXAppInstalled]) {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未安装微信，请安装后重新尝试" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alert show];
                        return ;
                    }
                    [[WeChat sharedInstance]sendAuthRequest:^(id result)
                     {
                         if (!result)
                         {
                             ITLog(@"获取权限失败");
                             return ;
                         }
                         NSString *newrequestStr = [NSString stringWithFormat:@"%@/user/getunionid.json?code=%@",SERVICE_ADRESS,[WeChat sharedInstance].loginResp.code];
                         AFHTTPRequestOperationManager *newManaer = [AFHTTPRequestOperationManager manager];
                         [newManaer GET:newrequestStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                             NSDictionary *newjson = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
                             if ([[NSString stringWithFormat:@"%@",newjson[@"result"]] integerValue]==0) {
                                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:newjson[@"errormsg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                 [alert show];
                             }else{
                                 [_scenceModel common_fullnamed:^(BOOL isFull) {
                                     if (isFull == YES) {
                                         [_reqmodel user_withdrawMoneyWithMoney:_yuELabel.text andCB:^(id output) {
                                             
                                         }];
                                     }else{
                                         [NavHelper gotoRealNameVCWithType:1];
                                     }
                                 }];
                             }
                         } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
                             
                         }];
                     }];
                }else if([[NSString stringWithFormat:@"%@",json[@"isExist"]] integerValue]==1){
                    [_reqmodel user_withdrawMoneyWithMoney:_yuELabel.text andCB:^(id output) {
                        NSLog(@"%@",output);
                        if ([[NSString stringWithFormat:@"%@",[output objectForKey:@"result"]] integerValue]==1) {
                            TiXianSuccessVC *tixianVC = [[TiXianSuccessVC alloc] init];
                            [tixianVC nickName:nickname andMoney:tixianjine];
                            [self.navigationController pushViewController:tixianVC  animated:YES];
                            
                        }else{
                            [AlertDialogHelper showToast:[output objectForKey:@"errormsg"]];
                        }
                    }];
                }
                
            } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
                
            }];

            
        }else{
            [NavHelper gotoRealNameVCWithType:1];
        }
    }];
    
    
}
- (void)textFieldEditChanged:(UITextField *)textField{
    if (textField.text.length>0) {
        self.tixianBtn.enabled = YES;
    }else{
        self.tixianBtn.enabled = NO;
    }
}
@end
